<template>
  <el-card class="form-container" shadow="never" style="width: 600px;">
    <el-form :model="homeAdvertise" prop="homeAdvertise" ref="homeAdvertise" label-width="150px" size="small"
      :rules="rules" style="width: 500px;">
      <!-- 分界线-->
      <el-divider>
        <span style=" font-size: 25px; font-weight: bold;color: #67C23A;">个人账号注册</span>
      </el-divider>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="homeAdvertise.username" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="homeAdvertise.password" class="input-width" type="password" show-password></el-input>
      </el-form-item>

      <el-form-item label="真实名" prop="name">
        <el-input v-model="homeAdvertise.name" class="input-width"></el-input>
      </el-form-item>

      <el-form-item label="身份证" prop="icCode">
        <el-input v-model="homeAdvertise.icCode" class="input-width"></el-input>
      </el-form-item>

      <el-form-item label="手机号码" prop="linkTel">
        <el-input v-model="homeAdvertise.linkTel" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="qq邮箱" prop="email">
        <el-input v-model="homeAdvertise.email" class="input-width"></el-input>
      </el-form-item>

      <el-form-item label="省份">
        <el-select v-model="homeAdvertise.province">
          <el-option v-for="item in selectList" :key="item.id" :label="item.title" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上传头像">
        <el-upload list-type="picture" :limit="1" action="http://localhost:8083/file/uploadUser"
          :on-success="handle_success">
          <!-- 再次提醒，:on-success="函数名"，函数名后面不要跟括号 -->
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip"></div>
        </el-upload>
      </el-form-item>
      <div style="display: flex;">
        <el-form-item>
          <el-button type="success" @click="handleSave()" round :disabled="isOk">确定</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="toHomepage()" type="primary" round>取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  </el-card>
</template>

<script>
import newAxios from "@/utils/axios.js";
export default {
  name: "HotelDetail",
  data() {
    return {
      userChangeForm: {},
      homeAdvertise: {},

      dialogOfUpload: false,
      fileList: [],

      selectList: [],
      user: {},
      isOk: false,

      //表单校验规则
      rules: {
        username: [
          {
            required: true,
            message: "请输入1-10位用户名",
            trigger: "blur,change",
          },
          {
            min: 1,
            max: 10,
            message: "长度在 1到 10个字符",
            trigger: "blur,change",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "change,blur",
          },
          {
            pattern: /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{8,}$/,
            message: "密码必须包含英文字母和数字，且长度大于或等于8位",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入2到 20位名字",
            trigger: "change",
          },
          {
            min: 2,
            max: 20,
            message: "长度在 2到 20 个字符",
            trigger: "blur",
          },
        ],
        icCode: [
          {
            required: true,
            message: "请输入身份证号码",
            trigger: "change",
          },
          {
            pattern: /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/,
            message: "身份证号码格式错误",
            trigger: "blur",
          },
        ],

        linkTel: [
          {
            required: true,
            message: "请输入手机号码",
            trigger: "change",
          },
          {
            pattern: /^1\d{10}$/,
            message:'手机号码格式错误',
            trigger:'blur'
          }
        ],

        email: [
          {
            required: true,
            message: "请输入qq邮箱",
            trigger: "blur,change",
          },
          {
            pattern: /^[1-9]\d{7,}@qq\.com$/,
            message: "qq邮箱格式错误",
            trigger: "blur",
          },
        ],
      }
    };
  },
  //组件传参

  //钩子函数
  created() {
    this.findProvince();
    this.handleSave();
  },
  methods: {
    toHomepage() {
      this.$router.push({ path: "/homepage" });
    },

    //上传图片
    handle_success(res) {
      return (this.homeAdvertise.icon = res.data);
    },
    findProvince() {
      newAxios
        .get("/admin/findProvinceAll")
        .then((res) => {
          // console.log(res.data);
          this.selectList = res.data.map((item) => {
            console.log(item);
            return { id: item.province, title: item.addr };
          });
        })
        .catch((err) => {
          this.$message("修改数据处理失败");
        });
    },
    //1.保存用户·信息
    handleSave() {
      //1.校验表单是否正确
      this.$refs.homeAdvertise.validate((valid) => {
        //2.判断效验是否通过
        if (valid) {
          // 通过校验着请求后端
          //   debugger;
          this.$message({
            message: "正在发送注册信息到你的邮箱，请稍后...",
            type: "warning",
          });
          this.isOk = true;
          newAxios
            .post("/admin/saveOrUpdateUser", {
              ...this.homeAdvertise,
            })
            .then((res) => res.data)
            .then((res) => {
              console.log(res.data);
              this.$message.success("注册成功，即将返回登界面！");
              //返回登录
              this.$router.replace("/loginUser");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          this.$message({
            message: "请检查数据是否正确",
            type: "warning",
          });
        }
      });
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
